package ai.people.netmon.userconfig.controller;

import ai.people.netmon.framework.domain.userconfig.entity.UserCollectConfigAccount;
import ai.people.netmon.framework.domain.userconfig.request.AccountQueryRequest;
import ai.people.netmon.framework.exception.enums.CommonEnum;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.framework.utils.ResultUtil;
import ai.people.netmon.userconfig.service.UserCollectConfigAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户采集配置账号接口
 *
 * @author yuanqinglong
 * @date 2022/2/28 10:10
 */
@RestController
@RequestMapping("/account")
@Validated
public class AccountController {
    @Autowired
    UserCollectConfigAccountService service;

    /**
     * 分页查询接口
     *
     * @param request 请求
     * @return {@link Result}<{@link PageResponse}<{@link UserCollectConfigAccount}>>
     */
    @PostMapping("/page")
    Result<PageResponse<UserCollectConfigAccount>> page(@Valid @RequestBody AccountQueryRequest request){
        PageResponse<UserCollectConfigAccount> response = service.pageQuery(request);
        return ResultUtil.success(response);
    }

    /**
     * 通过id获取账号
     *
     * @param id id
     * @return {@link Result}<{@link UserCollectConfigAccount}>
     */
    @RequestMapping("/{id}")
    Result<UserCollectConfigAccount> getAccountById(@PathVariable("id") Long id){
        UserCollectConfigAccount account = service.getAccountById(id);
        return ResultUtil.success(account);
    }

    /**
     * 保存账号
     *
     * @param account 账号
     * @return {@link Result}<{@link Boolean}>
     */
    @PostMapping("/save")
    Result<Boolean> saveAccount(@Valid @RequestBody UserCollectConfigAccount account){
        service.saveAccount(account);
        return ResultUtil.success();
    }

    /**
     * 批量保存账户
     *
     * @param list 列表
     * @return {@link Result}<{@link Boolean}>
     */
    @PostMapping("/saveBatch")
    Result<Boolean> saveAccountBatch(@Valid @RequestBody List<UserCollectConfigAccount> list){
        service.saveAccountBatch(list);
        return ResultUtil.success();
    }

    /**
     * 更新账号
     *
     * @param account 账号
     * @return {@link Result}<{@link Boolean}>
     */
    @PutMapping
    Result<Boolean> updateAccount(@RequestBody UserCollectConfigAccount account){
        if (account == null || account.getId() == null) {
            return ResultUtil.error(CommonEnum.CHECK_NOT_EMPTY_PRIMARY_ID);
        }
        service.updateAccount(account);
        return ResultUtil.success();
    }

    /**
     * 删除账号
     *
     * @param id id
     * @return {@link Result}<{@link Boolean}>
     */
    @DeleteMapping("/{id}")
    Result<Boolean> delAccount(@PathVariable("id") Long id){
        service.delAccountById(id);
        return ResultUtil.success();
    }
}
